<?php
	include_once "xAP-functions/webincludes.php";
$type=intval($_GET["type"]);
$id=intval($_GET["id"]);
$dt=intval($_GET["dt"]);
$item=array();

if($type==0){
	//date picker
	$dt=date("Y-m-d");
	for($i=0;$i<20;$i++){
		$item[]=array("caption"=>$dt,
					  "url"=>"iphone_cctv.php?type=1&id=".$id."&dt=".strtotime($dt)."#_PicturesList1");
	$dt=date("Y-m-d",strtotime($dt." - 1 day"));
	$title="Date";
	}
}
if($type==1){
	//build todat ref
	for($i=0;$i<24;$i++){
		$today[$i]["count"]=0;
	}
	// add in stats
	$sql="SELECT count(itemid) as cid,hour(Createddatestamp) as hr FROM xap_pictures_item
	where createddatestamp between '".date("Y-m-d",$dt)." 00:00:00' and '".date("Y-m-d",$dt)." 23:59:59' and  SourceID=".$id."
	group by hour(Createddatestamp)";
	$results=dbquery($sql);
	foreach($results as $v){
		$today[$v["hr"]]["count"]=$v["cid"];
	}	
	
	//time picker
	for($i=0;$i<24;$i++){
		
		
		$st=strtotime(date("Y-m-d",$dt)." ".str_pad($i,2,"0",STR_PAD_LEFT).":00:00");
		if($i==23){
			$en=strtotime(date("Y-m-d",$dt)." 23:59:59");
			}else{
		$en=strtotime(date("Y-m-d",$dt)." ".str_pad(($i+1),2,"0",STR_PAD_LEFT).":00:00");
			}
		if(intval($today[$i]["count"])>0){
		//$today[$i]["name"]="<a href='#' OnClick='LoadCCTVData(".$id.",".$st.",".$en.")'>".str_pad($i,2,"0",STR_PAD_LEFT).":00</a><BR>";	
		$item[]=array("caption"=>$i.":00 > ".($i+1).":00",
					  "url"=>"iphone_cctv_image.php?id=".$id."&dt=".$dt."&hr=".$i."&st=".$st."&en=".$en."#_PicturesImage");		
			}
		
		

				  
		//reverse order
		krsort($item);
		//$dt=date("Y-m-d",strtotime($dt." - 1 day"));
		$title="Time";
	}
}




$q="?";
header("Content-type: text/xml");
?>
<<?=$q?>xml version="1.0" encoding="UTF-8"?>
<root>
<title set="waPicturesList<?=$type?>"><?=$title?></title>
<part>
<destination mode="replace" zone="waPicturesList<?=$type?>"  create="true"/>
<data><![CDATA[
<div class="iMenu">
<UL class='iArrow'>
<? foreach($item as $k=>$v):?>
<LI class='iArrow'><a href='/Pictures/<?=$v["url"]?>' rev="async"><?=$v["caption"];?></a></LI>
<? endforeach; ?>
</UL>
</div>
]]></data>
</part>
</root>